﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace OmegleConversationBuilder
{
    public partial class ConversationBuilder : Form
    {
        public List<ConversationPartControl> parts;

        public ConversationBuilder(List<ConversationPartControl> conversationParts, ArrayList conversationInfos)
        {
            InitializeComponent();

            Icon = OmegleStormer.Properties.Resources.omegle;

            this.parts = conversationParts;

            foreach (ConversationPartControl.ConversationData conversationInfo in conversationInfos)
            {
                AddNewPart(new ConversationPartControl(conversationInfo));
            }

            if (parts.Count == 0) AddNewPart();

            UpdatePartCountChange();
            FormClosing += ConversationBuilder_FormClosing;
        }

        void ConversationBuilder_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        void AddNewPart(ConversationPartControl newPart = null)
        {
            if (newPart == null)
                newPart = new ConversationPartControl();
            newPart.Dock = DockStyle.Fill;
            newPart.Removed += newPart_Removed;
            newPart.Moved += newPart_Moved;
            newPart.panelBar.MouseMove += newPart_MouseMove;
            parts.Add(newPart);
            UpdatePartCountChange();
        }

        int tableIndexOfMoving = -1;
        bool controlMoved = false;
        ConversationPartControl movingPart;

        void newPart_MouseMove(object sender, MouseEventArgs e)
        {
            movingPart = (sender as Control).Parent as ConversationPartControl;
            if (movingPart.DragAndDropInfo.MoveEnabled)
            {
                if (!controlMoved)
                {
                    tableIndexOfMoving = tableLayoutPanelParts.Controls.IndexOf(movingPart);
                    movingPart.Dock = DockStyle.None;
                    movingPart.Height = movingPart.MinimumSize.Height;
                    movingPart.Width = tableLayoutPanelParts.Width;
                    panelScrollWrapper.Controls.Add(movingPart);
                    movingPart.BringToFront();
                }
                var newLocation = Point.Subtract(panelScrollWrapper.PointToClient(Cursor.Position), new Size(movingPart.DragAndDropInfo.Offset));
                movingPart.Location = new Point(3, newLocation.Y);
                controlMoved = true;
            }
        }

        void newPart_Moved(ConversationPartControl sender)
        {
            if (controlMoved)
            {
                controlMoved = false;
                int partsHeight = tableLayoutPanelParts.Height / parts.Count;
                int scrollDifference = Point.Subtract(tableLayoutPanelParts.PointToClient(Cursor.Position), new Size(panelScrollWrapper.PointToClient(Cursor.Position))).Y;
                int cursorY = tableLayoutPanelParts.PointToClient(Cursor.Position).Y + scrollDifference;
                int remainder;
                int swapPartnerIndex = Math.DivRem(cursorY, partsHeight, out remainder);

                if (swapPartnerIndex != tableIndexOfMoving && swapPartnerIndex <= tableLayoutPanelParts.Controls.Count)
                {
                    parts.RemoveAt(tableIndexOfMoving);
                    parts.Insert(swapPartnerIndex, movingPart);
                }
                movingPart.Dock = DockStyle.Fill;
                UpdatePartCountChange();
            }
        }

        void UpdatePartCountChange()
        {
            float rowWidthPercent = (float)(100 / parts.Count);
            tableLayoutPanelParts.Controls.Clear();
            tableLayoutPanelParts.RowStyles.Clear();
            tableLayoutPanelParts.RowCount = parts.Count;

            for (int i = 0; i < parts.Count; i++)
            {
                tableLayoutPanelParts.RowStyles.Add(new RowStyle(System.Windows.Forms.SizeType.Percent, rowWidthPercent));
                tableLayoutPanelParts.Controls.Add(parts[i], 0, i);
            }
            UpdateScrolling();
        }

        void newPart_Removed(ConversationPartControl sender)
        {
            if (parts.Count > 1)
            {
                parts.Remove(sender);
                UpdatePartCountChange();
            }
        }

        void UpdateScrolling()
        {
            int properHeight = tableLayoutPanelParts.RowCount * (tableLayoutPanelParts.Controls[0].MinimumSize.Height + 39);
            if (properHeight > panelScrollWrapper.Height)
            {
                if (tableLayoutPanelParts.Dock == DockStyle.Fill)
                {
                    tableLayoutPanelParts.Dock = DockStyle.None;
                    tableLayoutPanelParts.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                    tableLayoutPanelParts.Location = new Point(0, 0);
                    tableLayoutPanelParts.Width = panelScrollWrapper.Width - 15;
                }
                tableLayoutPanelParts.Height = properHeight;
            }
            else if (tableLayoutPanelParts.Dock != DockStyle.Fill)
                tableLayoutPanelParts.Dock = DockStyle.Fill;
        }

        private void buttonAddNewPart_Click(object sender, EventArgs e)
        {
            AddNewPart();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {

        }

        private void ConversationBuilder_Resize(object sender, EventArgs e)
        {
            UpdateScrolling();
        }

    }
}
